package com.jincp.filepicker.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;

import com.jincp.filepicker.bean.ImageFile;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * @ProjectName: filepicker.
 * @ClassName: ImagePreviewPageAdapter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/3/22 23:26.
 */
public class ImagePreviewPageAdapter extends PagerAdapter {

    private Activity activity;
    private List<ImageFile> imageList = new ArrayList<>();

    public ImagePreviewPageAdapter(Activity activity, List<ImageFile> imageList) {
        this.activity = activity;
        this.imageList = imageList;
    }

        @Override
    public Object instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(activity);
            photoView.setEnabled(true);
            photoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            ImageFile imageFile = imageList.get(position);
            Glide.with(activity)
                    .load(imageFile.getPath())
                    .into(photoView);
            container.addView(photoView);
        return photoView;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
